from socket import *
from threading import Thread

class Handle(Thread):
    def __init__(self,connfd):
        self.connfd=connfd
        super().__init__()
    def run(self):
        while True:
            data=self.connfd.recv(1024).decode()
            if not data :
                 break
            print(data)
            self.connfd.send(b"OK")
        self.connfd.close()


class ThreadServer():
    def __init__(self,*,host="",port=0):
        self.host=host
        self.port=port
        self.address=(host,port)
        self.sock=self._create_socket()

    def _create_socket(self):
        tcp_socket=socket()
        tcp_socket.bind(self.address)
        return tcp_socket

    # 服务启动函数
    def main(self):
        self.sock.listen(5)
        print("Listen the port %d" %self.port)
        # 循环链接客户端
        while True:
            connfd, addr =self.sock.accept()
            print("Connfd from", addr)
            handle=Handle(connfd)
            handle.start()

if __name__ == '__main__':
    server=ThreadServer(host="0.0.0.0",port=8888)
    server.main()





































































